<template>
    <el-dialog title="提示" v-model="dialog.isdialog">
        <p>{{ dialog.title }}</p>
        <p>{{ dialog.content }}</p>
        <el-button @click="dialog.isdialog = false">确定</el-button>
    </el-dialog>
    <div class="login-container">
        <div class="login-form">
            <h2 class="login-title">用户登录</h2>
            <el-form :model="form" :rules="rules" ref="loginForm" label-width="80px">
                <el-form-item prop="account">
                    <label for="account">账号：</label>
                    <el-input v-model="form.account" placeholder="请输入名称" required></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <label for="password">密码：</label>
                    <el-input type="password" placeholder="请输入密码" v-model="form.password" required></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" class="login-button" @click="submitForm">登录</el-button>
                </el-form-item>
            </el-form>
            <a href="register" class="register-link">注册</a>
            <!-- 使用 GitHub 图标 -->
            <el-button type="primary" class="github-button" @click="loginWithGitHub">
                <i class="fab fa-github github-icon" alt="GitHub Login"></i> <!-- FontAwesome GitHub 图标 -->
            </el-button>
        </div>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { ref } from 'vue';
import { useUserStore } from '@/stores/user';

const user = useUserStore();

const form = ref({
    account: '',
    password: ''
});

const rules = ref({
    account: [
        { required: true, message: '请输入账号', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' }
    ]
});
const dialog = ref({
    isdialog: false,
    title: '',
    content: ''
});

function submitForm() {
    axios.post('/api/users/login', {
        username: form.value.account,
        password: form.value.password
    }).then(res => {
        console.log(res);
        
        if (res.data.code === 200) {
            user.loginstatus = true;
            user.token = res.data.data.token; 
            user.userInfo = res.data.data.user;
            dialog.value = {
                isdialog: true,
                title: '成功',
                content: '登录成功'
            };
            // 登录成功后跳转到首页
            window.location.href = '/';
        } else {
            dialog.value = {
                isdialog: true,
                title: '错误',
                content: '登录失败，请检查您的账号和密码。'
            };
        }
    }).catch(error => {
        dialog.value = {
            isdialog: true,
            title: '错误',
            content: '登录请求失败，请稍后再试。'
        };
        console.error('登录请求出错:', error);
    });
}

function loginWithGitHub() {
    // 跳转到后端的 GitHub 登录端点
    window.location.href = 'http://localhost:3000/user/auth/github';
}
</script>

<style>
.login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    width: 100%;
}

.login-form {
    background-color: #fff;
    border-radius: 8px;
    padding: 20px;
    width: 500px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}

.login-title {
    text-align: center;
    margin-bottom: 20px;
    color: #333;
}

.el-input {
    margin-right: 20px;
}

.login-button {
    display: block;
    width: 100%;
    padding: 10px;
    margin-top: 10px;
    background-color: #333;
    color: #fff;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 16px;
}

.login-button:hover {
    background-color: #555;
}

/* GitHub 登录按钮样式 */
.github-button {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 10%;
    padding: 10px;
    margin-top: 10px;
    background-color: #333;
    color: #fff;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 16px;
}

.github-icon {
    width: 20px; /* 设置图标宽度为20px */
    height: 20px; /* 设置图标高度为20px */
}

.github-button:hover {
    background-color: #555;
}

/* 样式注册链接 */
.register-link {
    display: block;
    text-align: right;
    margin-top: 10px;
    text-decoration: none;
    color: #333;
}
</style>
